<?php
/*---------------------------------------------------------------------
 * 数据库访问模型model dao实现
 * ---------------------------------------------------------------------
 * Copyright (c) 2013-now http://blog518.com All rights reserved.
 * ---------------------------------------------------------------------
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * ---------------------------------------------------------------------
 * Author: <yangjian102621@gmail.com>
 * @version 1.2.1
 *-----------------------------------------------------------------------*/

namespace app\models;

use herosphp\core\Loader;
use herosphp\model\C_Model as BaseCModel;



class C_Model extends BaseCModel {

    public function __construct( $table, $config = null ) {
		parent::__construct($table,$config);
    }

	/*
    public function query($sql)
    public function insert($data)
    public function replace($data)
    public function delete($id)
    public function deletes($conditions)
    public function getItems($conditions, $fields, $order, $page, $pagesize, $group, $having)
    public function getItem($conditions, $fields, $order, $group, $having)
    public function update($data, $id)
    public function updates($data, $conditions)
    public function count($conditions)
    public function increase($field, $offset, $id)
    public function batchIncrease($field, $offset, $conditions)
    public function reduce($field, $offset, $id)
    public function batchReduce($field, $offset, $conditions)
    public function set($field, $value, $id)
    public function sets($field, $value, $conditions)
    public function beginTransaction()
    public function commit()
    public function rollback()
    public function inTransaction()
    protected function loadFilterData(&$data) {
    public function setMapping($mapping)
    public function getMapping()
    public function setPrimaryKey($primaryKey)
    public function getPrimaryKey()
    public function setFilterMap($filter)
    public function getFilterMap()
    public function setTable($table) {
    public function getTableMapping()
    public function setTableMapping($tableMapping)
    public function getDB() {
	*/
	public function insert($data)
	{
		$data = $this->filterData($data);

		if(!isset($data['addtime'])){
			$data['addtime'] = date("Y-m-d H:i:s");
		}
		if(!isset($data['edittime'])){
			$data['edittime'] = date("Y-m-d H:i:s");
		}
		if(!isset($data['app_id']))
		{
			$data['app_id'] = appId();
		}
		if(!isset($data['admin_id'])){
			$data['admin_id'] = adminId();
		}
		return parent::insert($data);
	}
	public function update($data, $id)
	{
		$data = $this->filterData($data);
		if(!isset($data['edittime'])){
			$data['edittime'] = date("Y-m-d H:i:s");
		}
		if(!isset($data['app_id']))
		{
			$data['app_id'] = appId();
		}
		if(!isset($data['admin_id'])){
			$data['admin_id'] = adminId();
		}
		return parent::update($data,$id);
	}
    public function updates($data, $conditions)
	{
		$data = $this->filterData($data);
		if(!isset($data['edittime'])){
			$data['edittime'] = date("Y-m-d H:i:s");
		}
		if(!isset($data['app_id']))
		{
			$data['app_id'] = appId();
		}
		if(!isset($data['admin_id'])){
			$data['admin_id'] = adminId();
		}
		return parent::updates($data,$conditions);
	}
	private function filterData($data)
	{
		foreach($data as $k => $v)
		{
			if(is_array($v))
			{
				$data[trim($k,'[]')] = implode(',',$v);
			}
			if($k == 'status' && $v == 'on')
			{
				$data[$k] = 1;
			}
		}
		//$data['addtime'] = date("Y-m-d H:i:s");
		//$data['modifiedtime'] = date("Y-m-d H:i:s");
		return $data;
	}
}
?>
